import type { FoodI, FoodPlanI, GetFoodListParamsI } from '@/types/food'
import {
  queryFoodListByName,
  queryFoodFilters,
  queryFoodListByIds,
  queryFoodTotal,
  queryFoodFiltersNotLimit,
} from '@/AGC/manager/food'
import { useUserStore } from '@/stores/user'
import { getFoodCollectIds } from '@/AGC/utils/user'
import type { UserFiltersI } from '@/types/user'
import { getFoodDislikeIdsUtil } from '@/AGC/utils/dislike'

/**
 * 根据食物名称获取食物列表
 * @param name 食物名称
 * @returns 食物列表
 */
export const getFoodListByNameUtil = async (name: string) => {
  const list = await queryFoodListByName(name)

  return list.map((item) => {
    return {
      name: item.getName()!,
      weight: item.getWeight()!,
      calories: item.getCalories()!,
      protein: item.getProtein()!,
      fat: item.getFat()!,
      carbohydrate: item.getCarbohydrate()!,
    } as FoodI
  })
}

/**
 * 根据饮食计划获取食物列表
 * @param foodPlan 饮食计划
 * @returns 食物列表
 */
export const getFoodListByFoodPlanUtil = async (foodPlan: FoodPlanI) => {
  // 1. 根据目标和用餐场景获取食物计划列表
  const foodFilters = await queryFoodFiltersNotLimit(useUserStore().userFilter as UserFiltersI)

  // 2. 获取食物 ids
  let foodIds: number[] = []

  if (foodPlan.time == '早餐') {
    foodIds = foodFilters
      .filter((item) => item.getBreakfast() == 1)
      .map((item) => item.getFood_id()!)
  } else if (foodPlan.time == '午餐') {
    foodIds = foodFilters.filter((item) => item.getLunch() == 1).map((item) => item.getFood_id()!)
  } else if (foodPlan.time == '晚餐') {
    foodIds = foodFilters.filter((item) => item.getDinner() == 1).map((item) => item.getFood_id()!)
  }

  // 3. 获取用户不喜欢的食物 ids
  const foodDislikeIds = await getFoodDislikeIdsUtil()

  // 4. 获取用户喜欢的食物 ids
  const foodLikeIds = foodIds.filter((item) => !foodDislikeIds.includes(item))

  // 5. 获取食物列表
  let foodList = await queryFoodListByIds(foodLikeIds)

  // 6. 根据热量筛选
  foodList = foodList.filter((item) => {
    const calories = (foodPlan.weight / item.getWeight()) * item.getCalories()
    return calories >= foodPlan.calories - 20 && calories <= foodPlan.calories + 20
  })

  // 7. 排除自身
  foodList = foodList.filter((item) => item.getName() != foodPlan.food_name)

  // 8. 返回食物列表
  return foodList.map((item) => {
    return {
      id: item.getId()!,
      name: item.getName()!,
      weight: item.getWeight()!,
      unit: item.getUnit()!,
      calories: item.getCalories()!,
      protein: item.getProtein()!,
      fat: item.getFat()!,
      carbohydrate: item.getCarbohydrate()!,
      status: 0,
    } as FoodI
  })
}

/**
 * 根据食物名称获取食物ids
 * @param name 食物名称
 * @returns 食物ids
 */
export const getFoodIdsByNameUtil = async (name: string) => {
  const foodList = await queryFoodListByName(name)

  if (foodList.length > 0) {
    return foodList.map((food) => food.getId()!)
  }
}

/**
 * 获取需要展示的食物列表
 * @param foodFilter 食物筛选条件
 * @returns 需要展示的食物列表
 */
export const getFoodListUtil = async (foodFilter: GetFoodListParamsI) => {
  let ids: number[] | undefined = undefined

  if (foodFilter.name) {
    // 获取食物表中的 ids
    ids = await getFoodIdsByNameUtil(foodFilter.name)
  }

  // 获取食物筛选条件集合
  const foodFilters = await queryFoodFilters(foodFilter, ids)

  // 获取食物筛选表中的 ids
  if (foodFilters.length > 0) {
    ids = foodFilters.map((foodFilter) => foodFilter.getFood_id()!)
  } else {
    ids = []
  }

  // 获取真正展示的食物集合
  const tempFoodList = await queryFoodListByIds(ids)
  const user_id = useUserStore().userInfo.id
  const foodCollectIds = await getFoodCollectIds(user_id)

  return tempFoodList.map((food) => {
    return {
      id: food.getId()!,
      name: food.getName()!,
      weight: food.getWeight()!,
      unit: food.getUnit()!,
      calories: food.getCalories()!,
      protein: food.getProtein()!,
      fat: food.getFat()!,
      carbohydrate: food.getCarbohydrate()!,
      status: foodCollectIds.includes(food.getId()!) ? 1 : 0,
    }
  })
}

/**
 * 获取食物筛选条件总数
 * @param foodFilter 食物筛选条件
 * @returns 食物筛选条件总数
 */
export const getFoodTotalUtil = async (foodFilter: GetFoodListParamsI) => {
  let ids: number[] | undefined = undefined

  if (foodFilter.name) {
    ids = await getFoodIdsByNameUtil(foodFilter.name)
  }

  return await queryFoodTotal(foodFilter, ids)
}
